package com.pyb.module.ogin.utils;

import android.util.Log;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMChatRoomManager;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMUserInfo;
import com.pyb.java.cache.CacheUserInfo;

/**
 * 环信工具类
 */
public class EaseIMUtils {

    private static EaseIMUtils mInstance;
    public static EaseIMUtils getInstance() {
        if(mInstance == null) {
            synchronized (EaseIMUtils.class) {
                if(mInstance == null) {
                    mInstance = new EaseIMUtils();
                }
            }
        }
        return mInstance;
    }
    /**
     * 获取group manager
     * @return
     */
    public EMGroupManager getGroupManager() {
        return getEMClient().groupManager();
    }
    /**
     * 获取chatroom manager
     * @return
     */
    public EMChatRoomManager getChatroomManager() {
        return getEMClient().chatroomManager();
    }
    /**
     * 获取IM SDK的入口类
     * @return
     */
    public EMClient getEMClient() {
        return EMClient.getInstance();
    }
    //删除当前会话的某条聊天记录
    public void deleteMsg(EMMessage message,String conversationId){
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(conversationId);
        conversation.removeMessage(message.getMsgId());
    }

    //根据会话插入消息
    public void insertMsg(EMMessage msg,String conversationId){
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(conversationId);
        conversation.insertMessage(msg);
    }

    //登录
    public void signin(String id,String password){
        EMClient.getInstance().login(id, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                Log.i("EaseIM","登录成功 ");
                EMUserInfo userInfo = new EMUserInfo();
                userInfo.setUserId(id);
                userInfo.setNickName(CacheUserInfo.getInstance().getsUserInfo()
                        .getUser().getRname());
//                userInfo.setAvatarUrl("https://img2.baidu.com/it/u=1070003001,653753576&fm=26&fmt=auto&gp=0.jpg");
//                userInfo.setAvatarUrl("https://upload-images.jianshu.io/upload_images/5809200-a99419bb94924e6d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240");
                userInfo.setAvatarUrl(CacheUserInfo.getInstance().getsUserInfo()
                        .getUser().getAvatar());
                EMClient.getInstance().userInfoManager().updateOwnInfo(userInfo, new EMValueCallBack<String>() {
                    @Override
                    public void onSuccess(String value) {
                        Log.i("EaseIM","用户信息设置成功 ");
                    }
                    @Override
                    public void onError(int error, String errorMsg) {
                        Log.e("EaseIM","用户信息设置失败 "+errorMsg);
                    }
                });
            }

            @Override
            public void onError(int code, String error) {
                Log.e("EaseIM","登录失败 "+ code+", "+ error);
            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });
    }

    //退出登录
    public void signout(){
        EMClient.getInstance().logout(false, new EMCallBack() {
            @Override
            public void onSuccess() {
                Log.i("EaseIM","退出登录成功 ");
            }

            @Override
            public void onError(int code, String error) {
                Log.e("EaseIM","退出登录失败 "+ code+", "+ error);
            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });
    }

}
